package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzq.annotation.EnableMyPage;
import com.zzq.domain.dto.SmilePhotographerWithdrawalRecordDTO;
import com.zzq.domain.entity.SmilePhotographer;
import com.zzq.domain.entity.SmilePhotographerDividendBill;
import com.zzq.domain.entity.SmilePhotographerWithdrawalRecord;
import com.zzq.domain.entity.SmileShootAddressApply;
import com.zzq.domain.search.PhotographerWithdrawalRecordSearchParamet;
import com.zzq.enumerate.AuditEnum;
import com.zzq.enumerate.WithdrawalStatusEnum;
import com.zzq.result.SmileResult;
import com.zzq.service.SmilePhotographerDividendBillService;
import com.zzq.service.SmilePhotographerService;
import com.zzq.service.SmilePhotographerWithdrawalRecordService;
import com.zzq.service.WithdrawAdminService;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class WithdrawAdminServiceImpl implements WithdrawAdminService {
    @Autowired
    SmilePhotographerWithdrawalRecordService photographerWithdrawalRecordService;
    @Autowired
    SmilePhotographerDividendBillService photographerDividendBillService;
    @Autowired
    SmilePhotographerService photographerService;
    @Override
    @EnableMyPage
    public SmileResult list(PhotographerWithdrawalRecordSearchParamet searchParamet) {
        List<SmilePhotographerWithdrawalRecordDTO> list = photographerWithdrawalRecordService.getWithdrawalRecordDTO(searchParamet);
        return SmileResult.sucess("获取成功",list);
    }


    @Override
    @Transactional
    public SmileResult setAudit(Long photographerWithdrawalRecordId, Boolean isby) {
        LambdaUpdateWrapper<SmilePhotographerWithdrawalRecord> queryWrapper = new LambdaUpdateWrapper();
        queryWrapper.eq(
                !ObjectUtils.isEmpty(photographerWithdrawalRecordId),
                SmilePhotographerWithdrawalRecord::getId,
                photographerWithdrawalRecordId);
        if (isby) {
            queryWrapper.set(SmilePhotographerWithdrawalRecord::getAudit, AuditEnum.AUDITED.getName());
        } else {
            queryWrapper.set(SmilePhotographerWithdrawalRecord::getAudit, AuditEnum.NOAUDITED.getName());
        }
        photographerWithdrawalRecordService.update(queryWrapper);
        return SmileResult.sucess("审核成功");
    }

    @Override
    public SmileResult getBankNo(Long photographerId) {
        SmilePhotographer photographer = photographerService.getById(photographerId);
        String photographerBankCard = photographer.getPhotographerBankCard();
        if (ObjectUtils.isEmpty(photographerBankCard)){
            return SmileResult.waring("摄影师无银行卡请完善信息");
        }
        return SmileResult.sucess("获取成功",photographer.getPhotographerBankCard());
    }

    @Override
    @Transactional
    public SmileResult withdrawPayment(Long photographerWithdrawalRecordId) {
        SmilePhotographerWithdrawalRecord record = photographerWithdrawalRecordService.getById(photographerWithdrawalRecordId);
        record.setWithdrawalStatus(WithdrawalStatusEnum.FINISH.getName());
        photographerWithdrawalRecordService.updateById(record);
        Long photographerId = record.getPhotographerId();
        List<SmilePhotographerDividendBill> billListByPhotographerId = photographerDividendBillService.getBillListByPhotographerId(photographerId, false);
        for (SmilePhotographerDividendBill smilePhotographerDividendBill : billListByPhotographerId) {
            smilePhotographerDividendBill.setWithdrawalStatus(true);
        }
        photographerDividendBillService.updateBatchById(billListByPhotographerId);
        return SmileResult.sucess("支付提现成功");
    }
}
